/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.util;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class StringUtils {
	
	/**
	 * 判断是否为空，空返回true
	 */
	public static boolean isEmpty(String str) {
		if (null == str)
			str = "";
		return "".equals(str);
	}
	
	public static boolean isEmptyL(String str) {
		if (null == str||"null".equals(str))
			str = "";
		return "".equals(str);
	}

	/**
	 * 判断是否不为空，不空返回true
	 */
	public static boolean isNotEmpty(String str) {
		return !isEmpty(str);
	}

	/**
	 * 判断两个字符串是否相等
	 * @param str1
	 * @param str2
	 * @return 相等返回true
	 */
	public static boolean isEqual(String str1, String str2) {
		if(isEmpty(str1) && isEmpty(str2))
			return true;
		else if(isEmpty(str1) || isEmpty(str2)) 
			return false;
		else 
			return str1.trim().equals(str2.trim());
	}
	
	 /**
     * 去除所有空格,包括中间的
     */
    public static String removeAllSpace(String str) {
        return str.replaceAll("\\s+", "");
    }
    
    /**
     * 判断字符串是否为空
     * @param strs
     * @return 不空返回true
     */
    public static boolean isNotEmpty(String[] strs) {
    	return strs != null && strs.length != 0;
    }
    /**
     * 去掉 resource中的空元素
     * @param resource
     * @return
     */
    public static String[] removeNull(String[] resource) {
    	List<String> target = new LinkedList<String>();
    	
    	if(!isNotEmpty(resource)) 
    		return null;
    	
    	for(String s : resource) {
    		if(isNotEmpty(s))
    			target.add(s);
    	}

    	return target.toArray(new String[target.size()]);
    }

    /**
	 * 获取resc - target的差集
	 * @param resc
	 * @param target
	 * @return
	 */
	public static List<String> getMinus(List<String> resc, List<String> target) {
		List<String> r = resc.subList(0, resc.size());

		for(String s : target){
			int j = 0;
			while(j < r.size()) {
				if(StringUtils.isEqual(r.get(j), s))
					r.remove(j);
				else 
					j++;
			}
		}
		
		return r;
	}
	
	/**
	 * 将数组转换成变长List，如果想要获取定长List，直接使用jdk方法：Arrays.asList(T ... a)
	 * @param array
	 * @return
	 */
	public static List<String> changeArrayToList(String[] array) {
		List<String> rescsList = new LinkedList<String>();
		
		if(!isNotEmpty(array))
			return rescsList;
		
		for(String s : array) {
			rescsList.add(s);
		}
		
		return rescsList;
	}
	
	/**
	 * 封装String类的split
	 * @param str
	 * @param symbol
	 * @return
	 */
	public static String[] split(String str, String symbol) {
		if(isEmpty(str))
			return null;
		
		return str.split(symbol);
	}
	
	/**
	 * 加密
	 * @param str
	 * @return
	 */
	public static String password(String str) {
		return MD5.getMD5(str);
	}
	
	/**
	 * 获取随机密码
	 * @return
	 */
	public static String getRandPassword() {
		Random rand = new Random();
		return String.valueOf(rand.nextInt(999999));
	}

	
	/**
	 * 对指定字符串加密
	 * @return
	 */
	public static String enCodeStr(String str)throws Exception {
		BASE64Encoder enc = new BASE64Encoder();
		enc.encode(str.getBytes());   
		return enc.encode(str.getBytes());
	}
	
	/**
	 * 对指定字符串解密
	 * @return
	 * @throws Exception 
	 */
	public static String decode (String str) throws Exception{ 
	    BASE64Decoder decoder = new sun.misc.BASE64Decoder();    
        byte[] temp = decoder.decodeBuffer(str);   
        return new String(temp).trim();   
    } 

	/**
	 * 获取本地时间
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static String getTimeStr(){
	  Date d = new Date();
	  //获取本地时间
	  String time = d.toLocaleString();
	  return time;
	}

}
